﻿using System.Collections.Generic;
using Eneta.Portal.Common;
using Microsoft.SharePoint;
using Eneta.Portal.Common.Definitions;
using System;

namespace Eneta.Portal.Site
{
    public class ListsFeatureReceiver : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web;
            if (properties.Feature.Parent is SPWeb)
                web = properties.Feature.Parent as SPWeb;
            else
                web = ((SPSite)properties.Feature.Parent).RootWeb;

            var list = web.Lists["Keywords"];

            if (HasField(web, "EnetaKeywords"))
            {
                var field = web.Fields["EnetaKeywords"] as SPFieldLookup;
                field.SchemaXml = field.SchemaXml.Replace(field.LookupList, list.ID.ToString());
                field.Update(true);
            }
            else
            {
                web.Fields.AddLookup("EnetaKeywords", list.ID, true);
                var field = web.Fields["EnetaKeywords"] as SPFieldLookup;
                field.LookupField = SPBuiltInFieldId.Title.ToString();
                field.Update(true);
                web.Update();
            }

            try
            {
                list = web.Lists["Regions"];

                if (HasField(web, "Offices"))
                {
                    var field = web.Fields["Offices"] as SPFieldLookup;
                    field.SchemaXml = field.SchemaXml.Replace(field.LookupList, list.ID.ToString());
                    field.AllowMultipleValues = true;
                    field.Update(true);
                }
                else
                {
                    web.Fields.AddLookup("Offices", list.ID, true);
                    var field = web.Fields["Offices"] as SPFieldLookup;
                    field.LookupField = SPBuiltInFieldId.Title.ToString();
                    field.AllowMultipleValues = true;
                    field.Update(true);
                    web.Update();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception " + ex);
            }

            //var folder = web.GetFolder("/SiteCollectionImages/users");
            //if (!folder.Exists)
            //{
            //    var imageList = web.Lists["SiteCollectionImages"];
            //    imageList.AddItem("/SiteCollectionImages/users", SPFileSystemObjectType.Folder);
            //}

            //folder = web.GetFolder("/SiteCollectionImages/companies");
            //if (!folder.Exists)
            //{
            //    var imageList = web.Lists["SiteCollectionImages"];
            //    imageList.AddItem("/SiteCollectionImages/companies", SPFileSystemObjectType.Folder);
            //}
        }

        public static bool HasField(SPWeb web, string fieldName)
        {
            foreach (SPField field in web.Fields)
            {
                if (field.InternalName == fieldName)
                {
                    return true;
                }
            }
            return false;
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }
    }
}